package com.quad.innovators.salesease.service.impl.task;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.converter.TaskAddressConverter;
import com.quad.innovators.salesease.enums.TaskAddressTypeEnum;
import com.quad.innovators.salesease.mapper.task.TaskAddressMapper;
import com.quad.innovators.salesease.model.entity.task.TaskAddress;
import com.quad.innovators.salesease.model.form.task.TaskAddressForm;
import com.quad.innovators.salesease.model.vo.task.TaskAddressVO;
import com.quad.innovators.salesease.service.task.TaskAddressService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/12/9 13:22
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class TaskAddressServiceImpl extends ServiceImpl<TaskAddressMapper, TaskAddress> implements TaskAddressService {

    private final TaskAddressConverter taskAddressConverter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addList(List<TaskAddressForm> taskAddressFormList, Integer addressType) {
        if (CollectionUtils.isEmpty(taskAddressFormList)){
            return false;
        }
        TaskAddressTypeEnum taskAddressTypeEnum = TaskAddressTypeEnum.useCodeGetEnum(addressType);
        List<TaskAddress> taskAddressList = taskAddressFormList.stream().map(model -> {
            TaskAddress taskAddress = taskAddressConverter.toEntity(model);
            if (TaskAddressTypeEnum.WAREHOUSE.equals(taskAddressTypeEnum)){
                taskAddress.setWarehouseCode(model.getAddressCode());
            } else if (TaskAddressTypeEnum.SHOP.equals(taskAddressTypeEnum)){
                taskAddress.setShopCode(model.getAddressCode());
            }
            return taskAddress;
        }).toList();
        return this.saveBatch(taskAddressList);
    }

    @Override
    public List<TaskAddressForm> getTaskAddressVOByTaskCode(String code, Integer addressType) {
        return this.baseMapper.getTaskAddressVOByTaskCode(code, addressType);
    }

    @Override
    public List<TaskAddressVO> getAddressListByTaskCodeList(List<String> taskCodeList, Boolean H5NotShowTaskAddress) {
        return this.baseMapper.getAddressListByTaskCodeList(taskCodeList, H5NotShowTaskAddress);
    }
}
